﻿//默认为兰州,如果未定义则为永登
#undef StaID_52889


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices;
//using Raize.CodeSiteLogging;
using System.Configuration;
using Pub;
using System.IO;
using System.Data;
using System.Text;
using SurferPub;

namespace Pub
{
    /// <summary>
    /// 降水分布图
    /// </summary>
    public static class CSurferImg
    {
        [DllImport("user32.dll", EntryPoint = "MessageBoxA")]

        static extern int MsgBox(int hWnd, string msg, string caption, int type);



        [DllImport("RainDrawDLL.dll")]
        static extern int LoadConfig(string ConnectionString, string ImgSavePath, string WorkPath);

        [DllImport("RainDrawDLL.dll")]
        static extern int MakeSurfImg(string PStartDate, string PEndDate, string PStartTimes, string PEndTimes, int i_chk_zero);

        [DllImport("SurferDLL.dll")]
        static extern int MakeSurfImg(string DataFile);

        public static string minVal = "";
        public static string maxVal = "";

        public static string ImgSavePath = "";
        public static string WorkPath = "";

#if StaID_52889
        const string sXMin = "102.7";
        const string sXMax = "104.6";
        const string sYMin = "35.5";
        const string sYMax = "37.1";
        const string sBlnFileEmpty = "lzkong.bln";
        const string sBlnFile = "lz.bln";

        const string sTabName_Rain = "P_RainCount";
        const string strSQL_StaIDs = "";
#else

        const string sXMin = "102.6";
        const string sXMax = "103.8";
        const string sYMin = "36.2";
        const string sYMax = "37.1";
        const string sBlnFileEmpty = "ydkong.bln";
        const string sBlnFile = "yd.bln";

        const string sTabName_Rain = "LZQX_DB.dbo.P_RainCount";
        const string strSQL_StaIDs = " AND StaID IN (SELECT StationID FROM LZQX_DB.dbo.P_RainStation_Config WHERE ParentID=45) ";
#endif



        #region 原始雨情绘图方法

        public static string getRainImg_D_01(DateTime dateS, DateTime dateE, string imgPath)
        {
            string SC = "08";
            int i_chk_zero = 0;

            string sDateS = dateS.ToString("yyyyMMdd") + SC;
            string sDateE = dateE.ToString("yyyyMMdd") + SC;

            return getRainImg_D(sDateS, sDateE, SC, imgPath, i_chk_zero);
        }

        public static string getRainImg_D(string dateS, string dateE, string SC, string imgPath, int i_chk_zero)
        {

            CDebug.EnterMethod("后台绘图");
            CDebug.Send(dateS + "," + dateE + "," + SC + "," + imgPath + ", " + i_chk_zero);

            string ImgFile = "";
            try
            {
                string ConnectionString = "Provider=SQLOLEDB.1;" + ConfigurationManager.AppSettings["DBCon"];
                string ImgSavePath = imgPath;
                //string RainDrawPath = ConfigurationManager.AppSettings["RainDrawPath"];
                string WorkPath = imgPath;
                if (LoadConfig(ConnectionString, ImgSavePath, WorkPath) == 0)
                    CDebug.Send("连接成功！");
                else
                    CDebug.Send("连接失败！");

                string StartDate = dateS;      //20120602
                string EndDate = dateE;        //20120603
                string StartTimes = SC;                                                 //08
                string EndTimes = SC;                                                   //08

                int imgTyp = MakeSurfImg(StartDate, EndDate, StartTimes, EndTimes, i_chk_zero);
                if (imgTyp == 0) ImgFile = "";
                else if (imgTyp == 1)
                {
                    ImgFile = imgPath + StartDate.Substring(0, 6) + "/" + EndDate.Substring(0, 8) + "_0808.jpg";
                }
                else if (imgTyp == 2)
                {
                    ImgFile = imgPath + StartDate.Substring(0, 6) + "/" + StartDate.Substring(0, 8) + "_" + EndDate.Substring(0, 8) + "_0808.jpg";
                }

                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("后台绘图");

            return ImgFile;
        }

        /// <summary>
        /// 降水分布图
        /// </summary>
        public static string getRainImg_D(DateTime dateS, DateTime dateE, string SC, string serverPath, string imgDir, int i_chk_zero)
        {
            CDebug.EnterMethod("生成降水分布图");
            //CDebug.Send(dateS + "," + dateE + "," + SC + "," + serverPath + ", " + i_chk_zero);

            string ImgFile = "";
            try
            {
                string ConnectionString = "Provider=SQLOLEDB.1;" + ConfigurationManager.AppSettings["DBCon"];
                string ImgSavePath = serverPath + imgDir;
                //string RainDrawPath = ConfigurationManager.AppSettings["RainDrawPath"];
                string WorkPath = serverPath + imgDir;
                if (LoadConfig(ConnectionString, ImgSavePath, WorkPath) == 0)
                    CDebug.Send("连接成功！");
                else
                    CDebug.Send("连接失败！");

                string StartDate = dateS.ToString("yyyyMMdd");      //20120602
                string EndDate = dateE.ToString("yyyyMMdd");        //20120603
                string StartTimes = SC;                                                 //08
                string EndTimes = SC;                                                   //08

                int imgTyp = MakeSurfImg(StartDate, EndDate, StartTimes, EndTimes, i_chk_zero);
                if (imgTyp == 0) ImgFile = "";
                else if (imgTyp == 1)
                {
                    ImgFile = imgDir + StartDate.Substring(0, 6) + "/" + EndDate + "_0808.jpg";
                }
                else if (imgTyp == 2)
                {
                    ImgFile = imgDir + StartDate.Substring(0, 6) + "/" + StartDate + "_" + EndDate + "_0808.jpg";
                }

                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("生成降水分布图");

            return ImgFile;
        }

        #endregion

        #region 新雨情绘图方法
        /// <summary>
        /// c#调用surfer画雨情图
        /// </summary>
        /// <param name="DatS"></param>
        /// <param name="DatE"></param>
        /// <param name="sTime"></param>
        /// <returns></returns>
        public static string getRainImg_CS(DateTime DatS, DateTime DatE, String sTime)
        {
            CDebug.EnterMethod("降水分布图I_C#");
            DataSet ds1 = null;
            string img = "";
            try
            {

                CADO ado1 = new CADO();
                //AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                string StartTime = DatS.ToString("yyyyMMdd") + sTime;
                string EndTime = DatE.ToString("yyyyMMdd") + sTime;
                //TimeSpan ts = new TimeSpan();
                //ts = DatE - DatS;
                //int DayCount = ts.Days;
                string strSQL = "select a.StationID as StaID,sum(Result) as Val,b.Lon,B.Lat from " + sTabName_Rain
                    + " a join P_STATION b on a.StationID = b.StaID where a.ProductDate > '" + StartTime
                    + "' and a.ProductDate <= '" + EndTime + "' and a.ParentID <> '10' "
                    + " AND A.ProductDate LIKE '%" + sTime + "' ";

                if (minVal != null && minVal != string.Empty)
                    strSQL += " AND Result >" + minVal;

                strSQL += " Group By a.StationID,b.Lon,B.Lat";


                CDebug.Send(strSQL);
                ds1 = ado1.sel1(strSQL);
                CDebug.Send("返回结果个数：" + ado1.ds_count);
                ado1.CloseDB();
                //CDebug.Send(ds1);

                //int i_chk_zero = 0;
                string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-"
                    + DatE.ToString("yyyy年MM月dd日") + sTime + "时降水分布图";

                RainDrawDLL.Units.RainDrawDLL.AppPath = WorkPath;
                RainDrawDLL.Units.RainDrawDLL.SavePath = ImgSavePath;
                RainDrawDLL.Units.RainDrawDLL.levelFileTemp = "雨情模板.lvl";
                RainDrawDLL.Units.RainDrawDLL.levelFile = "雨情.lvl";

                string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                    + "_" + minVal + "_" + maxVal + ".jpg";


                img = RainDrawDLL.Units.RainDrawDLL.MakeSurfImg(sImgTit, img_name, ds1);

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("降水分布图", ex);
            }
            CDebug.ExitMethod("降水分布图I_C#");

            return img;
        }

        public static string getRainImg_CS(DateTime DatS, DateTime DatE, String sTimeS, String sTimeE)
        {
            CDebug.EnterMethod("降水分布图II_C#");
            DataSet ds1 = null;
            string img = "";
            try
            {

                //CADO ado1 = new CADO();
                AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                if (sTimeS.Length < 2) sTimeS = "0" + sTimeS;
                if (sTimeE.Length < 2) sTimeE = "0" + sTimeE;

                string StartTime = DatS.ToString("yyyyMMdd") + sTimeS;
                string EndTime = DatE.ToString("yyyyMMdd") + sTimeE;
                //TimeSpan ts = new TimeSpan();
                //ts = DatE - DatS;
                //int DayCount = ts.Days;

                string sView = "HIS_HOUR_VIEW_" + DatS.ToString("yyyyMM") + "_" + DatE.ToString("yyyyMM");

                if (CCreatView.isView(sView) == false)
                    CCreatView.creatView(DatS, DatE, sView, "HIS_HOUR_", "yyyyMM");

                string strSQL = "select a.StationNum as StaID,SUM(Precipitation)"
                     + " as Val,b.Lon,B.Lat from " + sView
                     + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                     + StartTime + "' and a.ObservTimes <= '" + EndTime + "' "
                     + " AND Precipitation is not null "
                     + " AND B.lon is not null ";

                if (minVal != null && minVal != string.Empty)
                    strSQL += " AND Precipitation > " + minVal;
                if (maxVal != null && maxVal != string.Empty)
                    strSQL += " AND Precipitation <= " + maxVal;

                strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                CDebug.Send(strSQL);
                ds1 = ado1.sel1(strSQL);
                CDebug.Send("返回结果个数：" + ado1.ds_count);
                ado1.CloseDB();
                //CDebug.Send(ds1);

                //int i_chk_zero = 0;
                string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimeS + "时-"
                    + DatE.ToString("yyyy年MM月dd日") + sTimeE + "时降水分布图";

                RainDrawDLL.Units.RainDrawDLL.AppPath = WorkPath;
                RainDrawDLL.Units.RainDrawDLL.SavePath = ImgSavePath;
                RainDrawDLL.Units.RainDrawDLL.levelFileTemp = "雨情模板.lvl";
                RainDrawDLL.Units.RainDrawDLL.levelFile = "雨情.lvl";

                string img_name = DatS.ToString("yyyyMMdd") + sTimeS + "_" + DatE.ToString("yyyyMMdd") + sTimeE
                    + "_" + minVal + "_" + maxVal + ".jpg";


                img = RainDrawDLL.Units.RainDrawDLL.MakeSurfImg(sImgTit, img_name, ds1);

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("降水分布图", ex);
            }
            CDebug.ExitMethod("降水分布图II_C#");

            return img;
        }

        public static string getRainImg_D(DateTime DatS, DateTime DatE)
        {
            string sTime = "08";

            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-"
                + DatE.ToString("yyyy年MM月dd日") + sTime + "时降水分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                + "_" + minVal + "_" + maxVal + ".jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getRainImg_D(DateTime DatS, DateTime DatE, String sTime)
        {

            string sImgTit = "";

            sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-" + DatE.ToString("yyyy年MM月dd日") + sTime + "时降水分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                + "_" + minVal + "_" + maxVal + ".jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getRainImg_D(DateTime DatS, DateTime DatE, String sTimeS, String sTimeE)
        {

            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimeS + "时-"
                + DatE.ToString("yyyy年MM月dd日") + sTimeE + "时降水分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTimeS + "_" + DatE.ToString("yyyyMMdd") + sTimeE
                + "_" + minVal + "_" + maxVal + "_H.jpg";


            return getRainImg_D(DatS, DatE, sTimeS, sTimeE, sImgTit, img_name, "HOUR");
        }

        public static string getRainImg_D(DateTime DatS, DateTime DatE, String sTimeS, String sTimeE, string sTit, string sImgName, string sTyp)
        {
            CDebug.EnterMethod("降水分布图II_Delphi");
            DataSet ds1 = null;
            string img = "";
            try
            {
                if (sTimeS.Length < 2) sTimeS = "0" + sTimeS;
                if (sTimeE.Length < 2) sTimeE = "0" + sTimeE;

                if (sTyp == "DAY")
                {
                    CADO ado1 = new CADO();
                    //AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                    string StartTime = DatS.ToString("yyyyMMdd") + sTimeS;
                    string EndTime = DatE.ToString("yyyyMMdd") + sTimeE;
                    //TimeSpan ts = new TimeSpan();
                    //ts = DatE - DatS;
                    //int DayCount = ts.Days;
                    string strSQL = "select a.StationID as StaID,sum(Result) as Val,b.Lon,B.Lat from " + sTabName_Rain
                        + " a join P_STATION b on a.StationID = b.StaID where a.ProductDate >= '" + StartTime
                        + "' and a.ProductDate <= '" + EndTime + "' and a.ParentID <> '10' "
                        + " AND A.ProductDate LIKE '%" + sTimeS + "' ";

                    if (minVal != null && minVal != string.Empty)
                        strSQL += " AND Result >" + minVal;

                    strSQL += " Group By a.StationID,b.Lon,B.Lat";

                    strSQL = "SELECT * FROM (" + strSQL + ") A WHERE 1=1 " + strSQL_StaIDs + "";

                    CDebug.Send(strSQL);
                    ds1 = ado1.sel1(strSQL);
                    CDebug.Send("返回结果个数：" + ado1.ds_count);
                }

                if (sTyp == "HOUR")
                {
                    AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                    string StartTime = DatS.ToString("yyyyMMdd") + sTimeS;
                    string EndTime = DatE.ToString("yyyyMMdd") + sTimeE;

                    string sView = "HIS_HOUR_VIEW_" + DatS.ToString("yyyyMM") + "_" + DatE.ToString("yyyyMM");

                    if (CCreatView.isView(sView) == false)
                        CCreatView.creatView(DatS, DatE, sView, "HIS_HOUR_", "yyyyMM");

                    string strSQL = "select a.StationNum as StaID,SUM(Precipitation)"
                         + " as Val,b.Lon,B.Lat from " + sView
                         + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                         + StartTime + "' and a.ObservTimes <= '" + EndTime + "' "
                         + " AND Precipitation is not null "
                         + " AND B.Lon is not null ";

                    if (minVal != null && minVal != string.Empty)
                        strSQL += " AND Precipitation > " + minVal;
                    if (maxVal != null && maxVal != string.Empty)
                        strSQL += " AND Precipitation <= " + maxVal;

                    strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                    strSQL = "SELECT * FROM (" + strSQL + ") A WHERE 1=1 " + strSQL_StaIDs + "";

                    CDebug.Send(strSQL);
                    ds1 = ado1.sel1(strSQL);
                    CDebug.Send("返回结果个数：" + ado1.ds_count);
                    ado1.CloseDB();
                }


                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    if (ds1.Tables[0].Rows[i]["Val"].ToString() == "0.0")
                        ds1.Tables[0].Rows[i]["Val"] = "0";
                }

                //int i_chk_zero = 0;
                string sImgTit = sTit;
                if (sImgTit == null || sImgTit == string.Empty)
                {
                    if (DatS == DatE)
                    {
                        sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimeS + "时降水分布图";
                    }
                    else
                    {
                        sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimeS + "时-"
                            + DatE.ToString("yyyy年MM月dd日") + sTimeE + "时降水分布图";
                    }
                }


                string img_name = sImgName;
                if (img_name == null || img_name == string.Empty)
                    img_name = DatS.ToString("yyyyMMdd") + sTimeS + "_" + DatE.ToString("yyyyMMdd") + sTimeE
                        + "_" + minVal + "_" + maxVal + "_H.jpg";

                //生成dat文件
                List<string> lstDat = new List<string>();
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + " "
                            + dr1["Lat"].ToString() + " "
                            + dr1["Lon"].ToString() + " "
                            + "0 6 1 "
                            + dr1["val"].ToString();
                        lstDat.Add(s1);
                    }
                }
                string sDatFileName = WorkPath + @"SrfStandard\LANZHOU.DAT";
                CFile.Write(sDatFileName, lstDat, Encoding.Default, true);

                //生成绘图通用数据文件
                List<string> lstTemp = new List<string>();
                double dMin = 0;
                double dMax = 0;
                if (!CDB.dsIsNull(ds1))
                {
                    dMin = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    dMax = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + ","
                            + dr1["Lat"].ToString() + ","
                            + dr1["Lon"].ToString() + ","
                            + "0,"
                            + dr1["val"].ToString();
                        lstTemp.Add(s1);

                        if (Convert.ToDouble(dr1["Val"]) < dMin) dMin = Convert.ToDouble(dr1["Val"]);
                        if (Convert.ToDouble(dr1["Val"]) > dMax) dMax = Convert.ToDouble(dr1["Val"]);

                    }
                }

                if (dMax - dMin > 5)
                    dMin -= Math.Round((dMax - dMin) / 12, 1);
                else
                    dMin -= Math.Round((dMax - dMin) / 12, 2);

                CDebug.Send("Min=" + dMin + ", Max=" + dMax);

                surfData suf = new surfData();
                suf.sPath_PubData = WorkPath;
                suf.sImgFullName = ImgSavePath + img_name;
                suf.sStaIDFile = "LANZHOU.DAT";
                suf.sLvTemplateFile = "雨情模板.lvl";
                suf.sMaxVal = dMax.ToString();
                suf.sMinVal = dMin.ToString();
                suf.sOutLvFile = "雨情.lvl";
                suf.sBlnFileEmpty = sBlnFileEmpty;
                suf.sBlnFile = sBlnFile;
                suf.sXMin = sXMin;
                suf.sXMax = sXMax;
                suf.sYMin = sYMin;
                suf.sYMax = sYMax;
                suf.sImgTitle = sImgTit;
                suf.lstData = lstTemp;

                string sDataFile = suf.sImgFullName + ".txt";
                Suf.writeDataFile(sDataFile, suf);

                //CDebug.Send(sDataFile);
                MakeSurfImg(sDataFile);

                img = suf.sImgFullName;

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("降水分布图", ex);
            }
            CDebug.ExitMethod("降水分布图II_Delphi");

            return img;
        }


        public static string getRainImg_D_Mon(DateTime DatS)
        {
            String sTime = "08";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);

            string sImgTit = DatS.ToString("yyyy年MM月") + "降水分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + sTime + "_" + minVal + "_" + maxVal + "_M.jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getRainImg_D_Mon(DateTime DatS, DateTime DatE)
        {
            String sTime = "08";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-01"));
            DatE = DatE.AddMonths(1).AddDays(-1);

            string sImgTit = DatS.ToString("yyyy年MM月") + "-" + DatE.ToString("yyyy年MM月") + "降水分布图";

            string img_name = DatS.ToString("yyyyMM") + DatE.ToString("yyyyMM") + "_" + sTime + "_" + minVal + "_" + maxVal + "_M.jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getRainImg_D_Xun(DateTime DatS)
        {
            string sTime = "08";
            string sXun = "上旬";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);

            int iTyp = DatS.Day;

            if (iTyp <= 10)
            {
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-10"));
            }
            else if (iTyp > 10 && iTyp <= 20)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-11"));
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-20"));
                sXun = "中旬";
            }
            else if (iTyp > 20)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-21"));
                sXun = "下旬";
            }

            string sImgTit = DatS.ToString("yyyy年MM月") + sXun + "降水分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + iTyp + "_" + sTime + "_" + minVal + "_" + maxVal + "_X.jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getRainImg_D_Xun(DateTime DatS, string sTyp)
        {
            string sTime = "08";
            string sXun = "上旬";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);

            if (Convert.ToInt16(sTyp) == 1)
            {
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-10"));
            }
            else if (Convert.ToInt16(sTyp) == 2)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-11"));
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-20"));
                sXun = "中旬";
            }
            else if (Convert.ToInt16(sTyp) == 3)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-21"));
                sXun = "下旬";
            }

            string sImgTit = DatS.ToString("yyyy年MM月") + sXun + "降水分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + sTyp + "_" + sTime + "_" + minVal + "_" + maxVal + "_X.jpg";

            return getRainImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        #endregion



        #region 温度分布图

        public static string getTemImg_CS(DateTime DatS, DateTime DatE, String sTime)
        {
            CDebug.EnterMethod("温度分布图I_C#");
            DataSet ds1 = null;
            string img = "";
            try
            {

                //CADO ado1 = new CADO();
                AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                string StartTime = DatS.ToString("yyyyMMdd") + sTime;
                string EndTime = DatE.ToString("yyyyMMdd") + sTime;
                //TimeSpan ts = new TimeSpan();
                //ts = DatE - DatS;
                //int DayCount = ts.Days;
                string strSQL = "select a.StationNum as StaID,AVG(DryBulTemp)"
                    + " as Val,b.Lon,B.Lat from HIS_DAY_" + StartTime.Substring(0, 4)
                    + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                    + StartTime + "' and a.ObservTimes <= '" + EndTime + "' AND DryBulTemp is not null AND B.lon is not null "
                    + " AND a.ObservTimes LIKE '%" + sTime + "' ";

                if (minVal != null && minVal != string.Empty)
                    strSQL += " AND DryBulTemp >= " + minVal;
                if (maxVal != null && maxVal != string.Empty)
                    strSQL += " AND DryBulTemp <= " + maxVal;

                strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                CDebug.Send(strSQL);
                ds1 = ado1.sel1(strSQL);
                CDebug.Send("返回结果个数：" + ado1.ds_count);
                ado1.CloseDB();
                //CDebug.Send(ds1);

                //int i_chk_zero = 0;
                string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-"
                    + DatE.ToString("yyyy年MM月dd日") + sTime + "时平均温度分布图";

                RainDrawDLL.Units.RainDrawDLL.AppPath = WorkPath;
                RainDrawDLL.Units.RainDrawDLL.SavePath = ImgSavePath;
                RainDrawDLL.Units.RainDrawDLL.levelFileTemp = "温度模板.lvl";
                RainDrawDLL.Units.RainDrawDLL.levelFile = "温度.lvl";

                string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                    + "_" + minVal + "_" + maxVal + ".jpg";


                img = RainDrawDLL.Units.RainDrawDLL.MakeSurfImg(sImgTit, img_name, ds1);

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("温度分布图", ex);
            }
            CDebug.ExitMethod("温度分布图I_C#");

            return img;
        }

        public static string getTemImg_CS(DateTime DatS, DateTime DatE, String sTimS, String sTimE)
        {
            CDebug.EnterMethod("温度分布图II_C#");
            DataSet ds1 = null;
            string img = "";
            try
            {

                //CADO ado1 = new CADO();
                AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                if (sTimS.Length < 2) sTimS = "0" + sTimS;
                if (sTimE.Length < 2) sTimE = "0" + sTimE;

                string StartTime = DatS.ToString("yyyyMMdd") + sTimS;
                string EndTime = DatE.ToString("yyyyMMdd") + sTimE;
                //TimeSpan ts = new TimeSpan();
                //ts = DatE - DatS;
                //int DayCount = ts.Days;

                string sView = "HIS_HOUR_VIEW_" + DatS.ToString("yyyyMM") + "_" + DatE.ToString("yyyyMM");

                if (CCreatView.isView(sView) == false)
                    CCreatView.creatView(DatS, DatE, sView, "HIS_HOUR_", "yyyyMM");

                string strSQL = "SELECT a.StationNum as StaID,AVG(DryBulTemp)"
                    + " as Val,b.Lon,B.Lat from " + sView
                    + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                    + StartTime + "' and a.ObservTimes <= '" + EndTime
                    + "' AND DryBulTemp is not null AND B.lon is not null ";

                if (minVal != null && minVal != string.Empty)
                    strSQL += " AND DryBulTemp >= " + minVal;
                if (maxVal != null && maxVal != string.Empty)
                    strSQL += " AND DryBulTemp <= " + maxVal;

                strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                CDebug.Send(strSQL);
                ds1 = ado1.sel1(strSQL);
                CDebug.Send("返回结果个数：" + ado1.ds_count);
                ado1.CloseDB();
                //CDebug.Send(ds1);

                //int i_chk_zero = 0;
                string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimS + "时-"
                    + DatE.ToString("yyyy年MM月dd日") + sTimE + "时平均温度分布图";

                RainDrawDLL.Units.RainDrawDLL.AppPath = WorkPath;
                RainDrawDLL.Units.RainDrawDLL.SavePath = ImgSavePath;
                RainDrawDLL.Units.RainDrawDLL.levelFileTemp = "温度模板.lvl";
                RainDrawDLL.Units.RainDrawDLL.levelFile = "温度.lvl";

                string img_name = DatS.ToString("yyyyMMdd") + sTimS + "_" + DatE.ToString("yyyyMMdd") + sTimE
                    + "_" + minVal + "_" + maxVal + ".jpg";


                img = RainDrawDLL.Units.RainDrawDLL.MakeSurfImg(sImgTit, img_name, ds1);

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("温度分布图", ex);
            }
            CDebug.ExitMethod("温度分布图II_C#");

            return img;
        }

        public static string getTemImg_D(DateTime DatS, DateTime DatE)
        {
            string sTime = "20";
            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-"
                + DatE.ToString("yyyy年MM月dd日") + sTime + "时平均温度分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                + "_" + minVal + "_" + maxVal + ".jpg";

            return getTemImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getTemImg_D(DateTime DatS, DateTime DatE, String sTime)
        {

            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTime + "时-"
                + DatE.ToString("yyyy年MM月dd日") + sTime + "时平均温度分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTime + "_" + DatE.ToString("yyyyMMdd") + sTime
                + "_" + minVal + "_" + maxVal + ".jpg";

            return getTemImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getTemImg_D(DateTime DatS, DateTime DatE, String sTimS, String sTimE)
        {

            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimS + "时-"
                + DatE.ToString("yyyy年MM月dd日") + sTimE + "时平均温度分布图";

            string img_name = DatS.ToString("yyyyMMdd") + sTimS + "_" + DatE.ToString("yyyyMMdd") + sTimE
                + "_" + minVal + "_" + maxVal + ".jpg";

            return getTemImg_D(DatS, DatE, sTimS, sTimE, sImgTit, img_name, "HOUR");
        }

        public static string getTemImg_D(DateTime DatS, DateTime DatE, String sTimS, String sTimE, string sTit, string sImgName, string sTyp)
        {
            CDebug.EnterMethod("温度分布图II_Delphi");
            DataSet ds1 = null;
            string img = "";
            try
            {

                if (sTyp == "DAY")
                {
                    //CADO ado1 = new CADO();
                    AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                    string StartTime = DatS.ToString("yyyyMMdd") + sTimS;
                    string EndTime = DatE.ToString("yyyyMMdd") + sTimE;
                    //TimeSpan ts = new TimeSpan();
                    //ts = DatE - DatS;
                    //int DayCount = ts.Days;
                    string strSQL = "select a.StationNum as StaID,Round(AVG(DryBulTemp),1)"
                        + " as Val,b.Lon,B.Lat from HIS_DAY_" + StartTime.Substring(0, 4)
                        + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                        + StartTime + "' and a.ObservTimes <= '" + EndTime + "' AND DryBulTemp is not null AND B.lon is not null "
                        + " AND a.ObservTimes LIKE '%" + sTimS + "' ";

                    if (minVal != null && minVal != string.Empty)
                        strSQL += " AND DryBulTemp >= " + minVal;
                    if (maxVal != null && maxVal != string.Empty)
                        strSQL += " AND DryBulTemp <= " + maxVal;

                    strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                    strSQL = "SELECT * FROM (" + strSQL + ") A WHERE 1=1 " + strSQL_StaIDs + "";


                    CDebug.Send(strSQL);
                    ds1 = ado1.sel1(strSQL);
                    CDebug.Send("返回结果个数：" + ado1.ds_count);
                    ado1.CloseDB();
                    //CDebug.Send(ds1);
                }

                if (sTyp == "HOUR")
                {
                    //CADO ado1 = new CADO();
                    AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                    if (sTimS.Length < 2) sTimS = "0" + sTimS;
                    if (sTimE.Length < 2) sTimE = "0" + sTimE;

                    string StartTime = DatS.ToString("yyyyMMdd") + sTimS;
                    string EndTime = DatE.ToString("yyyyMMdd") + sTimE;
                    //TimeSpan ts = new TimeSpan();
                    //ts = DatE - DatS;
                    //int DayCount = ts.Days;

                    string sView = "HIS_HOUR_VIEW_" + DatS.ToString("yyyyMM") + "_" + DatE.ToString("yyyyMM");

                    if (CCreatView.isView(sView) == false)
                        CCreatView.creatView(DatS, DatE, sView, "HIS_HOUR_", "yyyyMM");

                    string strSQL = "SELECT a.StationNum as StaID,Round(AVG(DryBulTemp),1)"
                        + " as Val,b.Lon,B.Lat from " + sView
                        + " A join P_STATION b on a.StationNum = b.StaID where a.ObservTimes > '"
                        + StartTime + "' and a.ObservTimes <= '" + EndTime
                        + "' AND DryBulTemp is not null AND B.lon is not null ";

                    if (minVal != null && minVal != string.Empty)
                        strSQL += " AND DryBulTemp >= " + minVal;
                    if (maxVal != null && maxVal != string.Empty)
                        strSQL += " AND DryBulTemp <= " + maxVal;

                    strSQL += " Group By a.StationNum,B.Lon,B.Lat";

                    strSQL = "SELECT * FROM (" + strSQL + ") A WHERE 1=1 " + strSQL_StaIDs + "";

                    CDebug.Send(strSQL);
                    ds1 = ado1.sel1(strSQL);
                    CDebug.Send("返回结果个数：" + ado1.ds_count);
                    ado1.CloseDB();
                    //CDebug.Send(ds1);
                }


                //int i_chk_zero = 0;
                string sImgTit = sTit;
                if (sImgTit == null || sImgTit == string.Empty)
                    sImgTit = DatS.ToString("yyyy年MM月dd日") + sTimS + "时-"
                        + DatE.ToString("yyyy年MM月dd日") + sTimE + "时平均温度分布图";

                string img_name = sImgName;
                if (sImgName == null || sImgName == string.Empty)
                    sImgName = DatS.ToString("yyyyMMdd") + sTimS + "_" + DatE.ToString("yyyyMMdd") + sTimE
                        + "_" + minVal + "_" + maxVal + ".jpg";

                //生成dat文件
                List<string> lstDat = new List<string>();
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + " "
                            + dr1["Lat"].ToString() + " "
                            + dr1["Lon"].ToString() + " "
                            + "0 6 1 "
                            + dr1["val"].ToString();
                        lstDat.Add(s1);
                    }
                }
                string sDatFileName = WorkPath + @"SrfStandard\LANZHOU.DAT";
                CFile.Write(sDatFileName, lstDat, Encoding.Default, true);

                //生成绘图通用数据文件
                List<string> lstTemp = new List<string>();
                double dMin = 0;
                double dMax = 0;
                if (!CDB.dsIsNull(ds1))
                {
                    dMin = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    dMax = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + ","
                            + dr1["Lat"].ToString() + ","
                            + dr1["Lon"].ToString() + ","
                            + "0,"
                            + dr1["val"].ToString();
                        lstTemp.Add(s1);

                        if (Convert.ToDouble(dr1["Val"]) < dMin) dMin = Convert.ToDouble(dr1["Val"]);
                        if (Convert.ToDouble(dr1["Val"]) > dMax) dMax = Convert.ToDouble(dr1["Val"]);

                    }
                }

                surfData suf = new surfData();
                suf.sPath_PubData = WorkPath;
                suf.sImgFullName = ImgSavePath + img_name;
                suf.sStaIDFile = "LANZHOU.DAT";
                suf.sLvTemplateFile = "温度模板.lvl";
                suf.sMaxVal = dMax.ToString();
                suf.sMinVal = dMin.ToString();
                suf.sOutLvFile = "温度.lvl";
                suf.sBlnFileEmpty = sBlnFileEmpty;
                suf.sBlnFile = sBlnFile;
                suf.sXMin = sXMin;
                suf.sXMax = sXMax;
                suf.sYMin = sYMin;
                suf.sYMax = sYMax;
                suf.sImgTitle = sImgTit;
                suf.lstData = lstTemp;

                string sDataFile = suf.sImgFullName + ".txt";
                Suf.writeDataFile(sDataFile, suf);

                //CDebug.Send(sDataFile);
                MakeSurfImg(sDataFile);

                img = suf.sImgFullName;

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send(ds1);
                CDebug.Send("温度分布图", ex);
            }
            CDebug.ExitMethod("温度分布图II_Delphi");

            return img;
        }


        public static string getTemImg_D_Mon(DateTime DatS)
        {
            String sTime = "20";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);
            string sImgTit = DatS.ToString("yyyy年MM月") + "平均温度分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + sTime + "_" + minVal + "_" + maxVal + "_M.jpg";

            return getTemImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        public static string getTemImg_D_Mon(DateTime DatS, DateTime DatE)
        {
            String sTime = "20";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-01"));
            DatE = DatE.AddMonths(1).AddDays(-1);
            string sImgTit = DatS.ToString("yyyy年MM月") + "-" + DatE.ToString("yyyy年MM月") + "平均温度分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + DatE.ToString("yyyyMM") + "_" + sTime + "_" + minVal + "_" + maxVal + "_M.jpg";

            return getTemImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }


        public static string getTemImg_D_Xun(DateTime DatS, string sTyp)
        {
            string sTime = "20";
            string sXun = "上旬";
            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);

            if (Convert.ToInt16(sTyp) == 1)
            {
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-10"));
            }
            else if (Convert.ToInt16(sTyp) == 2)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-11"));
                DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-20"));
                sXun = "中旬";
            }
            else if (Convert.ToInt16(sTyp) == 3)
            {
                DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-21"));
                sXun = "下旬";
            }

            string sImgTit = DatS.ToString("yyyy年MM月") + sXun + "平均温度分布图";

            string img_name = DatS.ToString("yyyyMM") + "_" + sTyp + "_" + sTime + "_" + minVal + "_" + maxVal + "_X.jpg";

            return getTemImg_D(DatS, DatE, sTime, sTime, sImgTit, img_name, "DAY");
        }

        #endregion

        #region CI指数
        /// <summary>
        /// 绘制Ci指数分布图
        /// </summary>
        /// <returns></returns>
        public static string getCiImg(string serverPath, List<string> v)
        {
            CDebug.EnterMethod("Ci指数分布图");
            string ImgFile = "";

            try
            {
                string sTemDataFile = serverPath + CConfig.getPermite("imagesDir") + @"Pub\TemDataCi.txt";
                string sDataPath = serverPath + CConfig.getPermite("imagesDir");
                ImgFile = CConfig.getPermite("imagesDir") + @"Pub\bmp" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".bmp";

                List<string> lst = new List<string>();
                CFile.Read(sTemDataFile, ref lst);

                double d_max = Convert.ToDouble(v[0]);
                double d_min = Convert.ToDouble(v[0]);

                for (int i = 1; i < 4; i++)
                {
                    if (d_max < Convert.ToDouble(v[i])) d_max = Convert.ToDouble(v[i]);
                    if (d_min > Convert.ToDouble(v[i])) d_min = Convert.ToDouble(v[i]);
                }
                d_max += 0.1;
                d_min -= 0.1;

                d_max = Math.Round(d_max, 2);
                d_min = Math.Round(d_min, 2);

                lst[0] = lst[0].Replace("{DIR_SRF_DATA}", sDataPath);
                lst[1] = lst[1].Replace("{DIR_IMG}", serverPath + ImgFile);

                lst[4] = lst[4].Replace("{MAX}", "" + d_max);
                lst[5] = lst[5].Replace("{MIN}", "" + d_min);

                lst[14] = lst[14].Replace("{v1}", v[0]);
                lst[15] = lst[15].Replace("{v2}", v[1]);
                lst[16] = lst[16].Replace("{v3}", v[2]);
                lst[17] = lst[17].Replace("{v4}", v[3]);

                string sDataFile = serverPath + CConfig.getPermite("imagesDir") + @"/Pub/DataCi.txt";
                //File.Delete(sDataFile);
                CFile.Write(sDataFile, lst, Encoding.Default, true);
                //string sDataFileII = serverPath + CConfig.getPermite("imagesDir") + "DataCiII.txt";

                //File.Delete(sDataFileII);
                //CFile.UTF8_To_ASCII(sDataFile, sDataFileII);
                //File.Delete(sDataFile);

                //int imgTyp = MakeSurfImg(sDataFile);
                int imgTyp = CSurferPub.MakeSurfImg(sDataFile);
                CDebug.Send("绘图返回结果：" + imgTyp);


                ImgFile = ImgFile.Replace(@"\", "/");
                ImgFile = ImgFile.Replace("//", "/");

                CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("Ci指数分布图");

            return ImgFile;
        }

        #endregion


        #region 墒情分布图

        /// <summary>
        /// 绘制Ci指数分布图
        /// </summary>
        /// <returns></returns>
        public static string getMoisImg(string serverPath, DataSet ds1)
        {
            CDebug.EnterMethod("Ci指数分布图");
            string ImgFile_Bmp = "";
            string ImgFile_Jpg = "";

            try
            {
                string sTemDataFile = serverPath + CConfig.getPermite("imagesDir") + @"Pub\TemDataMois.txt";
                string sDataPath = serverPath + CConfig.getPermite("imagesDir");
                ImgFile_Bmp = CConfig.getPermite("imagesDir") + @"Pub\Mois_Tem" + DateTime.Now.ToString("HHmmss") + ".bmp";
                ImgFile_Jpg = CConfig.getPermite("imagesDir") + @"Pub\Mois" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg";

                File.Delete(serverPath + ImgFile_Bmp);
                List<string> lst = new List<string>();
                CFile.Read(sTemDataFile, ref lst);

                double d_max = Convert.ToDouble(ds1.Tables[0].Rows[0]["R"].ToString());
                double d_min = Convert.ToDouble(ds1.Tables[0].Rows[0]["R"].ToString());

                lst[0] = lst[0].Replace("{DIR_SRF_DATA}", sDataPath);
                lst[1] = lst[1].Replace("{DIR_IMG}", serverPath + ImgFile_Bmp);

                List<string> dat_lst = new List<string>();
                for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                {
                    DataRow dr1 = ds1.Tables[0].Rows[i];
                    string sLine1 = dr1["StaID"].ToString() + ","
                        + dr1["lat"].ToString() + ","
                        + dr1["lon"].ToString() + ","
                        + "0,"
                        + dr1["R"].ToString();
                    lst.Add(sLine1);

                    string s_v = dr1["R"].ToString();
                    if (d_max < Convert.ToDouble(s_v)) d_max = Convert.ToDouble(s_v);
                    if (d_min > Convert.ToDouble(s_v)) d_min = Convert.ToDouble(s_v);

                    string sLine2 = dr1["StaID"].ToString() + " "
                        + dr1["lat"].ToString() + " "
                        + dr1["lon"].ToString() + " "
                        + "0 6 12 "
                        + dr1["StaName"].ToString();
                    dat_lst.Add(sLine2);
                }

                string sStaFile = serverPath + CConfig.getPermite("imagesDir") + @"SrfStandard\Mois.dat";
                //File.Delete(sStaFile);
                CFile.Write(sStaFile, dat_lst, Encoding.Default, true);

                //d_max += 0.1;
                //d_min -= 0.1;

                d_max = Math.Round(d_max, 2);
                d_min = Math.Round(d_min, 2);

                lst[4] = lst[4].Replace("{MAX}", "" + d_max);
                lst[5] = lst[5].Replace("{MIN}", "" + d_min);

                string sDataFile = serverPath + CConfig.getPermite("imagesDir") + @"\Pub\DataMois.txt";
                File.Delete(sDataFile);
                CFile.Write(sDataFile, lst, Encoding.Default, true);
                //string sDataFileII = serverPath + CConfig.getPermite("imagesDir") + "DataCiII.txt";

                //File.Delete(sDataFileII);
                //CFile.UTF8_To_ASCII(sDataFile, sDataFileII);
                //File.Delete(sDataFile);

                //int imgTyp = MakeSurfImg(sDataFile);
                int imgTyp = CSurferPub.MakeSurfImg(sDataFile);
                CDebug.Send("绘图返回结果：" + imgTyp);

                ImgFile_Bmp = ImgFile_Bmp.Replace(@"\", "/");
                ImgFile_Bmp = ImgFile_Bmp.Replace("//", "/");

                ImgFile_Jpg = ImgFile_Jpg.Replace(@"\", "/");
                ImgFile_Jpg = ImgFile_Jpg.Replace("//", "/");

                if (File.Exists(serverPath + ImgFile_Jpg)) File.Delete(serverPath + ImgFile_Jpg);

                CPicture.ImageFormatter(serverPath + ImgFile_Bmp, serverPath + ImgFile_Jpg, "jpg");

                //CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("Ci指数分布图");

            return ImgFile_Jpg;
            //return ImgFile;
        }

        public static string getMoisImg(DateTime DatS, DateTime DatE, DataSet ds1)
        {
            CDebug.EnterMethod("Ci指数分布图");
            string ImgFile_Jpg = "";

            try
            {
                //CDebug.Send(ds1);

                string sImgTit = "";
                if (sImgTit == null || sImgTit == string.Empty)
                {
                    if (DatS == DatE)
                    {
                        sImgTit = DatS.ToString("yyyy年MM月dd日") + "土壤干旱度分布图";
                    }
                    else
                    {
                        sImgTit = DatS.ToString("yyyy年MM月dd日") + "-"
                            + DatE.ToString("yyyy年MM月dd日") + "土壤干旱度分布图";
                    }
                }


                string img_name = "";
                if (img_name == null || img_name == string.Empty)
                    img_name = DatS.ToString("yyyyMMdd") + "_" + DatE.ToString("yyyyMMdd") + "_DROUGHT.jpg";


                //生成dat文件
                List<string> lstDat = new List<string>();
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        ds1.Tables[0].Rows[i]["r"] = Math.Round(Convert.ToDouble(ds1.Tables[0].Rows[i]["r"].ToString()), 1);
                    }

                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + " "
                            + dr1["Lat"].ToString() + " "
                            + dr1["Lon"].ToString() + " "
                            + "0 6 1 "
                            + dr1["r"].ToString();
                        lstDat.Add(s1);
                    }
                }
                string sDatFileName = WorkPath + @"SrfStandard\LANZHOU.DAT";
                CFile.Write(sDatFileName, lstDat, Encoding.Default, true);

                //生成绘图通用数据文件
                List<string> lstTemp = new List<string>();
                double dMin = 0;
                double dMax = 0;
                if (!CDB.dsIsNull(ds1))
                {
                    dMin = Convert.ToDouble(ds1.Tables[0].Rows[0]["r"]);
                    dMax = Convert.ToDouble(ds1.Tables[0].Rows[0]["r"]);
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + ","
                            + dr1["Lat"].ToString() + ","
                            + dr1["Lon"].ToString() + ","
                            + "0,"
                            + dr1["r"].ToString();
                        lstTemp.Add(s1);

                        if (Convert.ToDouble(dr1["r"]) < dMin) dMin = Convert.ToDouble(dr1["r"]);
                        if (Convert.ToDouble(dr1["r"]) > dMax) dMax = Convert.ToDouble(dr1["r"]);

                    }
                }

                if (dMax - dMin > 5)
                    dMin -= Math.Round((dMax - dMin) / 12, 1);
                else
                    dMin -= Math.Round((dMax - dMin) / 12, 2);

                CDebug.Send("Min=" + dMin + ", Max=" + dMax);


                surfData suf = new surfData();
                suf.sPath_PubData = WorkPath;
                suf.sImgFullName = ImgSavePath + img_name;
                suf.sStaIDFile = "LANZHOU.DAT";
                suf.sLvTemplateFile = "Ci.lvl";
                suf.sMaxVal = dMax.ToString();
                suf.sMinVal = dMin.ToString();
                suf.sOutLvFile = "CiII.lvl";
                suf.sBlnFileEmpty = sBlnFileEmpty;
                suf.sBlnFile = sBlnFile;
                suf.sXMin = sXMin;
                suf.sXMax = sXMax;
                suf.sYMin = sYMin;
                suf.sYMax = sYMax;
                suf.sImgTitle = sImgTit;
                suf.lstData = lstTemp;

                string sDataFile = suf.sImgFullName + ".txt";
                Suf.writeDataFile(sDataFile, suf);

                //CDebug.Send(sDataFile);
                MakeSurfImg(sDataFile);

                ImgFile_Jpg = suf.sImgFullName;

                //CDebug.Send(ImgFile);
                //if (ImgFile != string.Empty )

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                CDebug.Send("生成图片出错", ex);
            }
            CDebug.ExitMethod("Ci指数分布图");

            return ImgFile_Jpg;
            //return ImgFile;
        }


        /// <summary>
        /// 墒情分布图
        /// </summary>
        /// <param name="DatS">yyyy-MM-dd</param>
        /// <param name="DatE">yyyy-MM-dd</param>
        /// <param name="sCol"></param>
        /// <returns></returns>
        public static string getMoisImg(DateTime DatS, DateTime DatE, String sTyp, string sTit)
        {
            CDebug.EnterMethod("墒情分布图I_Delphi");
            DataSet ds1 = null;
            string img = "";
            string sCol = "";
            try
            {
                switch (sTyp)
                {
                    case "10":
                        sCol = "S_10";
                        break;
                    case "20":
                        sCol = "S_20";
                        break;
                    case "30":
                        sCol = "S_30";
                        break;
                    case "0-30":
                        sCol = "(S_10 + S_20 + S_30) / 3";
                        break;
                    case "10-20":
                        sCol = "(S_10 + S_20) / 2";
                        break;
                    case "20-30":
                        sCol = "(S_20 + S_30) / 2";
                        break;
                }

                if (sCol == "")
                {
                    CDebug.ExitMethod("输入数据类型不正确: " + sTyp);
                    return "";
                }

                CADO ado1 = new CADO();
                //AdoMSSQL ado1 = new AdoMSSQL("DBcon_ZCD");

                string StartTime = DatS.ToString("yyyy-MM-dd 00:00:00");
                string EndTime = DatE.ToString("yyyy-MM-dd 00:00:00");
                //TimeSpan ts = new TimeSpan();
                //ts = DatE - DatS;
                //int DayCount = ts.Days;
                string strSQL = "SELECT * FROM ( "
                    + " select a.P_ID as StaID,AVG(" + sCol + ") as Val,b.Lon,B.Lat,B.PName from P_Moisture"
                    + " A JOIN P_MoisturePoint B ON A.P_ID = B.ID WHERE A.DDate >= '" + StartTime
                    + "' AND a.DDate <= '" + EndTime + "' "
                    + " Group By a.P_ID,b.Lon,B.Lat,B.PName "
                    + " ) A WHERE Val IS NOT NULL ";

                strSQL = "SELECT * FROM (" + strSQL + ") A WHERE 1=1 " + strSQL_StaIDs + "";

                CDebug.Send(strSQL);
                ds1 = ado1.sel1(strSQL);
                CDebug.Send("返回结果个数：" + ado1.ds_count);


                //CDebug.Send(ds1);

                //int i_chk_zero = 0;

                string sImgTit = "";
                if (sTit == null || sTit == "")
                    sImgTit = DatS.ToString("yyyy年MM月dd日") + "-"
                        + DatE.ToString("yyyy年MM月dd日") + sTyp + "cm墒情分布图";
                else
                    sImgTit = sTit;

                string img_name = DatS.ToString("yyyyMMdd") + "_" + DatE.ToString("yyyyMMdd") + "_" + sTyp
                    + ".jpg";

                //生成dat文件
                List<string> lstDat = new List<string>();
                if (!CDB.dsIsNull(ds1))
                {
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + " "
                            + dr1["Lon"].ToString() + " "
                            + dr1["Lat"].ToString() + " "
                            + "0 6 12 "
                            + dr1["PName"].ToString()
                            + Math.Round(Convert.ToDouble(dr1["val"].ToString()), 1);
                        lstDat.Add(s1);
                    }
                }
                string sDatFileName = WorkPath + @"SrfStandard\LANZHOU.DAT";
                CFile.Write(sDatFileName, lstDat, Encoding.Default, true);

                //生成绘图通用数据文件
                List<string> lstTemp = new List<string>();
                double dMin = 0;
                double dMax = 0;
                if (!CDB.dsIsNull(ds1))
                {
                    dMin = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    dMax = Convert.ToDouble(ds1.Tables[0].Rows[0]["Val"]);
                    for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr1 = ds1.Tables[0].Rows[i];
                        string s1 = dr1["StaID"].ToString() + ","
                            + dr1["Lon"].ToString() + ","
                            + dr1["Lat"].ToString() + ","
                            + "0,"
                            + Math.Round(Convert.ToDouble(dr1["val"].ToString()), 1);
                        lstTemp.Add(s1);

                        if (Convert.ToDouble(dr1["Val"]) < dMin) dMin = Convert.ToDouble(dr1["Val"]);
                        if (Convert.ToDouble(dr1["Val"]) > dMax) dMax = Convert.ToDouble(dr1["Val"]);

                    }
                    dMin = Math.Round(dMin, 1);
                    dMax = Math.Round(dMax, 1);
                }

                surfData suf = new surfData();
                suf.sPath_PubData = WorkPath;
                suf.sImgFullName = ImgSavePath + img_name;
                suf.sStaIDFile = "LANZHOU.DAT";
                suf.sLvTemplateFile = "0"; //"墒情.lvl";
                suf.sMaxVal = dMax.ToString();
                suf.sMinVal = dMin.ToString();
                suf.sOutLvFile = "墒情.lvl";
                suf.sBlnFileEmpty = sBlnFileEmpty;
                suf.sBlnFile = sBlnFile;
                suf.sXMin = sXMin;
                suf.sXMax = sXMax;
                suf.sYMin = sYMin;
                suf.sYMax = sYMax;
                suf.sImgTitle = sImgTit;
                suf.lstData = lstTemp;

                string sDataFile = suf.sImgFullName + ".txt";
                Suf.writeDataFile(sDataFile, suf);

                //CDebug.Send(sDataFile);
                MakeSurfImg(sDataFile);

                img = suf.sImgFullName;

            }
            catch (System.Exception ex)
            {
                CDebug.SendError(ex.ToString());
                //CDebug.Send(ds1);
                CDebug.Send("墒情分布图", ex);
            }
            CDebug.ExitMethod("墒情分布图I_Delphi");

            return img;
        }


        public static string getMoisImg_Mon(DateTime DatS, String sTyp)
        {

            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DateTime DatE = DatS.AddMonths(1).AddDays(-1);

            string sImgTit = DatS.ToString("yyyy年MM月") + sTyp + "cm墒情分布图";

            return getMoisImg(DatS, DatE, sTyp, sImgTit);
        }

        public static string getMoisImg_Mon(DateTime DatS, DateTime DatE, String sTyp)
        {

            DatS = Convert.ToDateTime(DatS.ToString("yyyy-MM-01"));
            DatE = Convert.ToDateTime(DatE.ToString("yyyy-MM-01"));
            DatE = DatE.AddMonths(1).AddDays(-1);

            string sImgTit = DatS.ToString("yyyy年MM月") + "-" + DatE.ToString("yyyy年MM月") + sTyp + "cm墒情分布图";

            return getMoisImg(DatS, DatE, sTyp, sImgTit);
        }


        public static string getMoisImg_Day(DateTime DatS, String sTyp)
        {

            DateTime DatE = DatS;

            string sImgTit = DatS.ToString("yyyy年MM月dd日") + sTyp + "cm墒情分布图";

            return getMoisImg(DatS, DatE, sTyp, sImgTit);
        }

        #endregion

        #region 日照时数分布图

        #endregion

    }

    /// <summary>
    /// surfer自动绘图需要数据
    /// </summary>
    public struct surfData
    {
        public string sPath_PubData;
        public string sImgFullName;
        public string sStaIDFile;
        public string sLvTemplateFile;
        public string sMaxVal;
        public string sMinVal;
        public string sOutLvFile;
        public string sBlnFileEmpty;
        public string sBlnFile;
        public string sXMin;//102.7
        public string sXMax;//104.6
        public string sYMin;//35.5
        public string sYMax;//37.1
        public string sImgTitle;//兰州市Ci指数分布
        public List<string> lstData;
        //52889,36.04,103.88,0,{v1}
        //52983,35.88,104.15,0,{v2}
        //52884,36.3,103.96,0,{v3}
        //52885,36.72,103.27,0,{v4}
    }

    /// <summary>
    /// surfer自动绘图
    /// </summary>
    public static class Suf
    {
        /// <summary>
        /// 将数据写入文件
        /// </summary>
        /// <param name="sDataFile"></param>
        /// <param name="sufData"></param>
        public static void writeDataFile(String sDataFile, surfData sufData)
        {
            List<string> lstTemp = new List<string>();
            lstTemp.Add(sufData.sPath_PubData);
            lstTemp.Add(sufData.sImgFullName);
            lstTemp.Add(sufData.sStaIDFile);
            lstTemp.Add(sufData.sLvTemplateFile);
            lstTemp.Add(sufData.sMaxVal);
            lstTemp.Add(sufData.sMinVal);
            lstTemp.Add(sufData.sOutLvFile);
            lstTemp.Add(sufData.sBlnFileEmpty);
            lstTemp.Add(sufData.sBlnFile);
            lstTemp.Add(sufData.sXMin);
            lstTemp.Add(sufData.sXMax);
            lstTemp.Add(sufData.sYMin);
            lstTemp.Add(sufData.sYMax);
            lstTemp.Add(sufData.sImgTitle);

            for (int i = 0; i < sufData.lstData.Count; i++)
            {
                lstTemp.Add(sufData.lstData[i]);
            }

            CFile.Write(sDataFile, lstTemp, Encoding.Default, true);
        }

    }

}
